package com.feng.rabbitmq.one;

import com.rabbitmq.client.*;

/**
 * author：feng
 * date: 2021-12-08 17:08
 * title：消费者-接收消息
 * error：
 *  Exception in thread "main" com.rabbitmq.client.AuthenticationFailureException:
 *  ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For
 *  etails see the broker logfile.
 * 错误原因： 密码输入错误
 */
public class Consumer {
    //队列名称
    //测试镜像队列
    public static final String QUEEN_NAME = "mirrior_hello";

    //接收消息
    public static void main(String[] args) throws Exception {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("admin");
        factory.setPassword("1234");
        factory.setHost("192.168.88.130");
        Connection connection = factory.newConnection();
        //用连接创建信道
        Channel channel = connection.createChannel();

        /**
         * 消费者消费消息
         *1.消费哪个队列
         * 2.消费成功之后是否要自动应答 true 代表的自动应答 false 代表手动应答
         * 3.消费者未成功消费的回调
         * 4.消费者取消消费的回调
         */
        //声明: 接收消息
        DeliverCallback deliverCallback = (consumerTag,message) ->{
//            System.out.println(message);
            System.out.println(new String(message.getBody()));
        };
        //取消消息时的回调
        CancelCallback cancelCallback = consumerTag ->{
            System.out.println("消费消息被中断");
        };

        channel.basicConsume(QUEEN_NAME,true,deliverCallback,cancelCallback);


    }
}
